﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Http;



namespace LeekGPTApp.Library.Services;

public class FundInfoService:IFundInfo
{
    public async Task<List<List<Dictionary<string, object>>>> getIn(String id)
    {
        List<List<Dictionary<string, object>>> resultLists = new List<List<Dictionary<string, object>>>();
        String getDetailUrl = "https://tiantian-fund-api.vercel.app/api/action?action_name=fundMNDetailInformation&FCODE=" + id;//获取基金详情
        resultLists.Add(await SendGetRequest(getDetailUrl, "Datas"));
        String Mname = (string)resultLists[0][0]["JJJL"];
        String getMidUrl = "https://tiantian-fund-api.vercel.app/api/action?action_name=fundSearch&m=7&key=" + Mname;//获取基金经理id
        resultLists.Add(await SendGetRequest(getMidUrl, "Datas"));
        String Mid = (string)resultLists[0][0]["MgrId"];
        string[] urls = new String[]{"https://tiantian-fund-api.vercel.app/api/action?action_name=fundGradeDetail&FCODE="+id,//评级
                                    "https://tiantian-fund-api.vercel.app/api/action?action_name=fundMNPeriodIncrease&RANGE=1n&FCODE="+id,//涨幅
                                    "https://tiantian-fund-api.vercel.app/api/action?action_name=fundVPageAcc&RANGE=1n&FCODE="+id,//一年累计收益
                                    "https://tiantian-fund-api.vercel.app/api/action?action_name=fundMSNMangerAcc&RANGE=1&mGRID="+Mid,//基金经理业绩走势
                                    "https://tiantian-fund-api.vercel.app/api/action?action_name=fundMSNMangerPerRank&mGRID="+Mid,//基金经理业绩排行
                                    "https://tiantian-fund-api.vercel.app/api/action?action_name=fundMSNMangerProContr&mGRID="+Mid};//基金经理持仓历史管理资金
        for (int i = 0; i < 6 && i != 2; i++)
        {
            resultLists.Add(await SendGetRequest(urls[i], "Datas"));
        }
        resultLists.Add(await SendGetRequest(urls[2], "data"));
        return resultLists;
    }
    public async Task<List<Dictionary<string, object>>> SendGetRequest(String url, String plc)
    {

        List<Dictionary<string, object>>? list = new List<Dictionary<string, object>>();
        try
        {
            using (HttpClient client = new HttpClient())
            {
                HttpResponseMessage response = await client.GetAsync(url);
                response.EnsureSuccessStatusCode();
                String json = await response.Content.ReadAsStringAsync();
                Dictionary<string, object>? resDic = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
                if (resDic != null)
                {
                    String? datas = resDic[plc].ToString();
                    if (datas != null)
                    {
                        if (datas.ToCharArray()[0] == '[')
                        {
                            list = JsonConvert.DeserializeObject<List<Dictionary<string, object>>>(datas);
                        }
                        else
                        {
                            Dictionary<string, object>? dic = JsonConvert.DeserializeObject<Dictionary<string, object>>(datas);
                            if (dic != null)
                            {
                                list.Add(dic);
                            }
                        }
                    }
                }


            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("An error occurred: " + ex.Message);
            list = new List<Dictionary<string, object>>();
        }
        if (list == null)
        {
            list = new List<Dictionary<string, object>>();
        }
        return list;
    }


}
